package fasilkom.antz.util;

import java.awt.Color;

import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.net.URL;

import com.golden.gamedev.engine.BaseIO;
import com.golden.gamedev.engine.BaseLoader;
import com.golden.gamedev.util.ImageUtil;

import fasilkom.antz.debug.Log;


/**
 * Kelas ini meng-override bapaknya dengan memodifikasi sistem loading image
 * sedemikian sehingga default tidak menggunakan masking dan menggunakan
 * translucent untuk transparency.
 *
 */
public class CustomizedLoader extends BaseLoader {

	public CustomizedLoader(BaseIO base, Color maskColor) {
		super(base, maskColor);
	}
	
	@Override
	public BufferedImage getImage(String imagefile, boolean useMask) {
		BufferedImage image = (BufferedImage) getStoredImage(imagefile);
		
		if (image == null) {
			URL url = getBaseIO().getURL(imagefile);
			
			image = (useMask) ? ImageUtil.getImage(url, getMaskColor())
			        : ImageUtil.getImage(url, Transparency.TRANSLUCENT);
			if (useMask)
				Log.d("use mask: " + imagefile);
			
			storeImage(imagefile, image);
		}
		
		return image;
	}
	
	/**
	 * Loads and returns an image with specified file using no masking color.
	 * Image that have been loaded before will return immediately from cache.
	 * 
	 * @see #getImage(String, boolean)
	 */
	@Override
	public BufferedImage getImage(String imagefile) {
		return this.getImage(imagefile, false);
	}
	
	@Override
	public BufferedImage[] getImages(String imagefile, int col,
			int row, boolean useMask) {
		BufferedImage[] image = (BufferedImage[]) getStoredImages(imagefile);
		
		if (image == null) {
			URL url = getBaseIO().getURL(imagefile);
			
			image = (useMask) ? ImageUtil.getImages(url, col, row,
			        getMaskColor()) : ImageUtil.getImages(url, col, row,
			        		Transparency.TRANSLUCENT);
			
			storeImages(imagefile, image);
		}
		
		return image;
	}
	
	/**
	 * Loads and returns image strip with specified file using no masking color.
	 * Images that have been loaded before will return immediately from cache.
	 * 
	 * @see #getImages(String, int, int, boolean)
	 */
	@Override
	public BufferedImage[] getImages(String imagefile, int col, int row) {
		return this.getImages(imagefile, col, row, false);
	}
}
